package com.FrameWork.Entity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.FrameWork.Utils.Utils;
import com.SFA.Main.HttpFactory;
import com.SFA.Main.MyUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Environment;

public class GlobalInfo
{
	private static GlobalInfo info = null;
	private static final String ConfigName="GLOBALINFO";
	public  static final String      platformCode = "Android";
	public static final String CABFILE="CABFILE";
	public  static final String  DOMAINID="DOMAINID";
	public  static final String  EMPLOYEECODE="EMPLOYEECODE";
	public  static final String  EMPLOYEEID =  "EMPLOYEEID";
	public  static final String  LASTSYNCTIME="LASTSYNCTIME";
	public  static final String	  USERNAME="USERNAME";
	public  static final String	  USERPASSWORD="USERPASSWORD";
	public  static final String	  USERID="USERID";
	public  static final String	  USERCODE="USERCODE";
	public  static final String	  SESSIONID="SESSIONID";
	public  static final String	PERSONID="PERSONID";
	public  static final String 	ORGANIZATIONID="ORGANIZATIONID";
	public  static final String	POSITIONCODE="POSITIONCODE";
	public  static final String	    SERVERTIME="SERVERTIME";
	public   static final String	    NAME="NAME";
	public   static final String	    USERTOKEN="UserToken";

	private String CabFile;
	private  int	       DomainID;
	private  String	       EmployeeCode;
	private  int	       EmployeeID;
	private  String	    LastSyncTime;
	private  String	    UserName;
	private  String	    UserPassword;
	private  String	    UserID;
	private  String	    UserCode;
	private  String	    SessionId;
	private  String      UserToken;
	private  int	PERSON_ID;
	private  int	       OrganizationID;
	private  String	    PositionCode;
	private  String	    ServerTime;
	private  String	    Name;
	public String  getCabFile()
	{
		return CabFile;
	}

	public int  getDomainID()
	{
		return DomainID;
	}

	public String  getEmployeeCode()
	{
		return EmployeeCode;
	}

	public int  getEmployeeID()
	{
		return EmployeeID;
	}

	public String  getLastSyncTime()
	{
		return LastSyncTime;
	}

	public String  getName()
	{
		return Name;
	}


	public int  getOrganizationID()
	{
		return OrganizationID;
	}

	public int  getPERSON_ID()
	{
		return PERSON_ID;
	}


	public String  getPositionCode()
	{
		return PositionCode;
	}

	public String  getServerTime()
	{
		return ServerTime;
	}

	public String  getSessionId()
	{
		return SessionId;
	}

	public String  getUserCode()
	{
		return UserCode;
	}

	public String  getUserID()
	{
		return UserID;
	}
	
	public String  getUserToken()
	{
		return UserToken;
	}

	public String  getUserName()
	{
		return UserName;
	}

	public String  getUserPassword()
	{
		return UserPassword;
	}

	public static GlobalInfo GetInstance(Context context)
	{
		if(info == null)
		{
			info = new GlobalInfo(context);
		}
		return info;
	}

	public boolean setValue(Context context, String key,Object value)
	{
		SharedPreferences sp = 
				context.getSharedPreferences(ConfigName,  Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		try
		{

			if(key.equals(CABFILE))
				this.CabFile = value==null ? null: (String)value;
			if(key.equals(DOMAINID))
				this.DomainID = value== null ?  0: (Integer)value;
			if(key.equals(EMPLOYEECODE))
				this.EmployeeCode = value==null ? null: (String)value;
			if(key.equals(EMPLOYEEID))
				this.EmployeeID = value== null ?  0: (Integer)value;
			if(key.equals(LASTSYNCTIME))
				this.LastSyncTime = value==null ? null: (String)value;
			if(key.equals(USERNAME))
				this.UserName = value==null ? null: (String)value;
			if(key.equals(USERPASSWORD))
				this.UserPassword = value==null ? null: (String)value;
			if(key.equals(USERID))
				this.UserID = value==null ? null: (String)value;
			if(key.equals(USERCODE))
				this.UserCode = value==null ? null: (String)value;
			if(key.equals(PERSONID))
				this.PERSON_ID = value== null ?  0: (Integer)value;
			if(key.equals(SESSIONID))
				this.SessionId = value==null ? null: (String)value;
			if(key.equals(ORGANIZATIONID))
				this.OrganizationID = value== null ?  0: (Integer)value;
			if(key.equals(POSITIONCODE))
				this.PositionCode = value==null ? null: (String)value;
			if(key.equals(SERVERTIME))
				this.ServerTime = value==null ? null: (String)value;
			if(key.equals(USERTOKEN))
				this.UserToken = value==null ? null: (String)value;
			if(key.equals(NAME))
				this.Name = value==null ? null: (String)value;


			if(value instanceof Boolean)
			{
				editor.putBoolean(key, value== null ? false: (Boolean)value);
			}
			else if(value instanceof Integer)
			{
				editor.putInt(key,value== null ?  0: (Integer)value);
			}
			else
			{
				editor.putString(key, value==null ? null: (String)value);
			}
			editor.commit();
		}
		catch(Exception err)
		{
			return false;
		}
		finally
		{
			editor = null;
			sp = null;
		}
		return true;
	}

	private GlobalInfo(Context context)
	{
		SharedPreferences sp = 
				context.getSharedPreferences(ConfigName,  Context.MODE_PRIVATE);
		UserCode = sp.getString(USERCODE,null);
		UserID=sp.getString(USERID,null);
		Name=sp.getString(NAME,null);
		DomainID=sp.getInt(DOMAINID,0);
		EmployeeCode=sp.getString(EMPLOYEECODE,null);
		EmployeeID=sp.getInt(EMPLOYEEID,0);
		CabFile = sp.getString(CABFILE,null);
		ServerTime= sp.getString(SERVERTIME,null);
		UserPassword =  sp.getString(USERPASSWORD, null);
		UserToken = sp.getString(USERTOKEN, null);
		sp = null;

	} 

	public static void SaveFunctionList(String Context)
	{
		
		//取得sd卡的目录 及要保存的文件名
		File file = new File(Utils.DIR, "FUNCTION");
		if (!file.getParentFile().exists())
			file.getParentFile().mkdirs();
		//取得文件的输出流
		FileOutputStream outStream=null;
		try {
			outStream = new FileOutputStream(file);
			//写入数据到sd卡
			outStream.write(Context.getBytes());

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if(outStream != null)
			{
				try {
					outStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				outStream = null;
			}
		}
	}

	public static List<FunctionEntity> GetFunctionList()
	{
		File f = new File(Environment.getExternalStorageDirectory()+ "/SFA" , "FUNCTION");
		if(!f.exists())
			return null;
		byte[] data = new byte[10240];
		String content = "";
		//取得文件的输出流
		FileInputStream inStream=null;
		try {
			inStream = new FileInputStream(f);
			if (inStream != null) 
			{
				InputStreamReader inputreader = new InputStreamReader(inStream);
				BufferedReader buffreader = new BufferedReader(inputreader);
				String line;
				//分行读取
				while (( line = buffreader.readLine()) != null) {
					content += line;
				}                
				inStream.close();
				inStream = null;
			}


		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if(inStream != null)
			{
				try {
					inStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				inStream = null;
			}
		}
		JSONArray array=null;
		try {
			array = new JSONArray(content);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		} 
		ArrayList<FunctionEntity> list = new ArrayList<FunctionEntity>();
		for (int i = 0; i < array.length(); i++)
		{
			JSONObject object;
			try {
				object = array.getJSONObject(i);

				String ImageName = object.getString("ImageResource");
				String LocalName = ImageName.substring(ImageName.lastIndexOf("/") + 1);
				FunctionEntity entity= new FunctionEntity();
				entity.ID = object.getString("ID");
				entity.ImageName= LocalName;
				entity.CODE= object.getString("Key");
				entity.Title = object.getString("NAME");
				entity.RSS = object.getString("RSSSource");
				entity.URL =entity.RSS ;
				entity.TYPE = object.getString("Type");
				entity.Number = i + 1;
				entity.ImageSource = ImageName;
				list.add(entity);
				// 下载图片
				try
				{
					if (!MyUtil.Exist(MyUtil.BuildFilePath(LocalName)))
					{
						Bitmap bitmap = HttpFactory.HttpFactorys.GetInputStream(ImageName);
						if (bitmap != null)
							MyUtil.saveBmpToSd(bitmap, LocalName, 100);

					}
				}
				catch (Exception err)
				{
					String str = err.getMessage();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


		}
		return list;
	}
}

